from multiprocessing import Process
from multiprocessing import Pool

import os
import time
import random
import subprocess


def start_lagou_scrapy():
    subprocess.run(["python3", "lagou/post.py"])


def start_boss_scrapy():
    subprocess.run(["python3", "boss/post.py"])


def start_qiancheng_scrapy():
    subprocess.run(["python3", "qiancheng/post.py"])


def start_dajie_scrapy():
    subprocess.run(["python3", "dajie/post.py"])


def start_offical_scrapy():
    for i in os.listdir('offical'):
        if not os.path.isdir(os.path.join('offical', i)):
            continue

        for j in os.listdir(os.path.join('offical', i)):
            filename = os.path.join('offical', i, j)
            if filename.split('.')[-1] == 'py':
                subprocess.run(['python3', filename])


m = {
    'lagou': start_lagou_scrapy,
    'boss': start_boss_scrapy,
    'qiancheng': start_qiancheng_scrapy,
    'dajie': start_dajie_scrapy,
    'offical': start_offical_scrapy
}

if __name__ == '__main__':
    from optparse import OptionParser

    parser = OptionParser(usage = "%prog [options]")
    parser.add_option("-p", "--platform", action = "store", type = "str", dest = "platform", help = "platform", default = 'qiancheng,lagou,boss,dajie')
    options, args = parser.parse_args()

    if not options.platform:
        exit(0)

    platform = options.platform.split(',')

    pool = []

    for p in platform:
        if not p in m:
            continue

        p = Process(target = m[p], args = ())
        pool.append(p)

    for p in pool:
        p.start()

    for p in pool:
        p.join()
